import axios from 'axios'

const api = axios.create({
    baseURL:'http://49.235.98.65:3000/',
    timeout:200000,
})

const get = async (url,data)=>{
    const response = await api.get(url,{params:data})
    .catch(err=>{alert(err.response.message||'接口报错')})
    return response.data
}
const post = async (url, data) => {
    console.log('===post===>', data);
    const response = await api.post(url,data)
    .catch(err=>{alert(err.response.message||'接口报错')})
    return response.data
}
const put = async (url,data)=>{
    const response = await api.put(url,data)
    .catch(err=>{alert(err.response.message||'接口报错')})
    return response.data
}

let del = async function (url, data) {
    const response = await api.request({data, url, method: 'delete'})
    .catch(err=>{alert(err.response.message||'接口报错')})
    return response.data;
}

// 添加请求拦截器
api.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    
    config.headers.user = JSON.parse(localStorage.getItem("a") || '{}')._id || '';
    config.headers.user = JSON.parse(localStorage.getItem("A") || '{}')._id || '';

    
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});


export {get,post,put,del}